home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 26.zip / BS1 part 26 / Aztec C v5.2a disk 2.adf / examples.lzh / getfile / getfile.c next >
C/C++ Source or Header  |  1991-10-23  |  16KB  |  530 lines

  1. /************************************************************************
  2. req.c
  3.     This file contains a general-purpose <requester> that
  4. will prompt the user for a filename input.
  5.     The program actually uses a window instead of a 'Requester'
  6. for greater flexibility. It will take control of your window's
  7. IDCMP Port when called.
  8.  
  9. ***    This material is copyright (c) 1986 by C. Heath of Microsmiths, Inc.
  10. Permission is granted to use these files in any way with the following
  11. exceptions:
  12.  
  13. 1) The files shall not be posted on any telecommunications service, public
  14. or private, except for BIX until January 15, 1987.
  15.  
  16. 2) The files may only be distributed in archive format, with no modifications.
  17. If you make any improvements on the file requester and would like to
  18. generally distribute them, please contact "cheath" on BIX, or write to:
  19.     Microsmiths Inc, PO Box 561, Cambridge, MA 02140
  20.  
  21. 3) The requester may be used in any commercial product, but must be in
  22. object code format.  You are free to make modifications for use in your
  23. product.  Permission is granted to Lattice, Inc, and to Manx, Inc, to
  24. include the source files in archive format.
  25.  
  26.     Thank you, and enjoy.
  27.         ...cheath
  28.  
  29. ************************************************************************/
  30.  
  31. #include "standard.h"
  32. #include "safeclose.h"
  33.  
  34. #define FAST register
  35. #define NL NULL
  36. static struct Window    *wRq = NL;        /* Requester window   */
  37.  
  38. extern char *dmore(), *dinit();
  39.  
  40. static struct FileLock      *pdir = NL;
  41. static struct FileInfoBlock *dir_info;
  42.  
  43. static struct Window   *eW;        /* Parent Window. Uck    */
  44.  
  45. static struct TextAttr MyFont = {(UBYTE *)"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT };
  46.  
  47. /* FGAD requires a few unique gadget-ids, origined at FGAD    */
  48. /* In the unlikely event this conflicts with any of your gadgets, */
  49. /* change this equate to allow 16 contiguous unique ID's    */
  50.  
  51. #define FGAD    0x76c0L
  52.  
  53. #define FCHARS    32L    /* Number of chars allowed in file name    */
  54.  
  55. #define DIR_SIZ    50L    /* Number of chars in a dir name...    */
  56. #define MAX_STR    DIR_SIZ+2L
  57.  
  58. #define DENTS    5L        /* Number of entries on screen   */
  59. /* It's not as simple as changing 'DENTS'...      */
  60.  
  61. #define DSIZE    FCHARS+1L    /* Size of a directory entry   */
  62.  
  63. #define    DBUFSIZ    3000L    /* Number of bytes to allocate for all ents */
  64.  
  65. #define BCOL    1L    /* Background color    */
  66. #define FCOL    2L    /* Foreground color    */
  67.  
  68. #define RHGHT    120L
  69. #define RWDTH    320L
  70.  
  71. #define ZWDTH    270L
  72.  
  73. static char    ubuf[MAX_STR];        /* Undo buffer    */
  74.  
  75. static struct dirent { struct dirent *next; BOOL isfile; char dE[DSIZE+2]; };
  76. static struct dirent *FirstEntry;
  77. static struct dirent *NextEntry;
  78. static struct dirhead { struct dirent *next; };
  79. static struct dirhead ListHead;
  80.  
  81. static long   curent,maxent;
  82. static BOOL   more;
  83.  
  84. static struct RastPort    *wRp;
  85.  
  86. /* Requester "Hailing" prompt */
  87. static struct IntuiText oTxt = {2,2,JAM1,10,11,NL, NL ,NL};
  88.  
  89. static struct IntuiText saydir = {0,1,JAM2,0,1,&MyFont,(UBYTE *)"(dir) ",NL};
  90.  
  91. static struct IntuiText rname[DENTS] = { /* File name list */
  92.    {2,1,JAM2,48,1,NL,NL, NL},
  93.    {2,1,JAM2,48,1,NL,NL, NL},
  94.    {2,1,JAM2,48,1,NL,NL, NL},
  95.    {2,1,JAM2,48,1,NL,NL, NL},
  96.    {2,1,JAM2,48,1,NL,NL, NL} };
  97.  
  98. /* Display for file name ... */
  99.  
  100. static SHORT oXY2[] = {-2,-2, RWDTH-78,-2, RWDTH-78,9, -2,9, -2,-2};
  101. static struct Border thebd = {0,0, 2,3,JAM1, 5,oXY2, NL};
  102.  
  103. static struct IntuiText otxt = {2,2,JAM1,-40,0,&MyFont,(UBYTE *)"file",NL};
  104. static struct StringInfo osx = { NL ,(UBYTE *)ubuf, NL,DSIZE,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  105. static struct Gadget ogx = { NL, 60,RHGHT-35,RWDTH-80 ,10,     /* File name gadget */
  106.    GADGHCOMP, RELVERIFY , STRGADGET,
  107.    (APTR)&thebd,NL,&otxt,NL,(APTR)&osx, FGAD+11,NL };
  108.  
  109. static struct Gadget oga = { &ogx, 10,70, ZWDTH,10,   /* Gadgets For   */
  110.    GADGHCOMP, RELVERIFY, BOOLGADGET,     /* Directory entries   */
  111.    NL,NL, &rname[4] ,NL,NL, FGAD+10,NL };
  112. static struct Gadget og9 = {&oga, 10,60, ZWDTH,10,
  113.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  114.    NL,NL, &rname[3] ,NL,NL, FGAD+9,NL };
  115. struct Gadget og8 = {&og9, 10,50, ZWDTH,10,
  116.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  117.    NL,NL, &rname[2] ,NL,NL, FGAD+8,NL };
  118. static struct Gadget og7 = {&og8, 10,40, ZWDTH,10,
  119.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  120.    NL,NL, &rname[1] ,NL,NL, FGAD+7,NL };
  121. static struct Gadget og6 = {&og7, 10,30, ZWDTH,10,
  122.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  123.    NL,NL, &rname[0] ,NL,NL, FGAD+6,NL };
  124.  
  125.  
  126. /* Gadjets for requester  */
  127.  
  128. static SHORT oXY3[] = {0,0, 50,0, 50,9, 0,9, 0,0};
  129. static SHORT oXY4[] = {2,-2, 48,-2, 52,2, 52,7, 48,11, 2,11, -2,7, -2,2, 2,-2};
  130. static struct Border obd2 = {0,0, 2,3,JAM1, 9,oXY4, NL};
  131. static struct Border obd1 = {0,0, 3,2,JAM1, 5,oXY3, &obd2};  /* OTAY / CANCEL box */
  132.  
  133. static struct IntuiText ot1 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"  OK  ",NL};
  134. static struct IntuiText ot2 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"Cancel",NL};
  135.  
  136. static struct IntuiText dtxt = {2,2,JAM1,-60,0,NL,(UBYTE *)"drawer",NL};
  137. static struct StringInfo os5 = { NL ,(UBYTE *)ubuf, NL,DIR_SIZ,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  138. static struct Gadget og5 = { &og6, RWDTH/2-80,19,190,10,     /* Directory */
  139.    GADGHCOMP, RELVERIFY, STRGADGET,
  140.    NL,NL,&dtxt,NL,(APTR)&os5, FGAD+5,NL };
  141.  
  142. static struct Image   cc_img;
  143. static struct PropInfo   cc_prop = {AUTOKNOB | FREEVERT, 0,0, 0,0x1000,0,0,0,0,0,0 };
  144. static struct Gadget og3 = { &og5,RWDTH-39,20,20,60,      /* Scroll Bar   */
  145.     GADGHCOMP,GADGIMMEDIATE | FOLLOWMOUSE, PROPGADGET,
  146.     (APTR)&cc_img,NL,NL,NL,(APTR)&cc_prop,FGAD+3,NL };
  147.  
  148. static struct Gadget og2 = { &og3, RWDTH-70,RHGHT-20, 50,10,  /* CANCEL */
  149.    GADGHCOMP,  RELVERIFY, BOOLGADGET,
  150.    (APTR)&obd1,NL, &ot2,NL,NL, FGAD+2,NL };
  151.  
  152. static struct Gadget og1 = { &og2, 20,RHGHT-20, 50,10,      /* OTAY   */
  153.    GADGHCOMP,        /* Flags    */
  154.    RELVERIFY,        /* Activation    */
  155.    BOOLGADGET,
  156.    (APTR)&obd1,NL,    /* GadgetRender, SelectRender    */
  157.    &ot1,NL,NL,        /* IntuiText, MutualExclude,SpecialInfo   */
  158.    FGAD+1,NL };        /* Gadget Id, UserData    */
  159.  
  160. /* Open a requester "Window" */
  161.  
  162. static struct NewWindow NewFiles = {
  163.    160, 30, RWDTH,RHGHT, BCOL,FCOL, NL, /* Fill in AFTER opening ... */
  164.    SMART_REFRESH | ACTIVATE | RMBTRAP | WINDOWDRAG,
  165.    &og1,NL,NL, NL,
  166.    NL, RWDTH,RHGHT,RWDTH,RHGHT, WBENCHSCREEN };
  167.  
  168. IMPORT struct Library    *IntuitionBase;
  169.  
  170.  
  171. static struct IntuiText b_txt = {0,1,JAM2, 5,20,NL,NL,    NL};
  172. static struct IntuiText p_txt = {0,1,JAM2, 5,3,NL,(UBYTE *)"OK", NL};
  173. #ifndef FOOP
  174. /****************************************************************/
  175. /* notify(txt)                     */
  176. /*   Prompts for Yes/No response            */
  177.  
  178. static MSnotify(txt)
  179.    char *txt;
  180.    {
  181.    b_txt.IText = (UBYTE *)txt;
  182.    AutoRequest(wRq,&b_txt,0L,&p_txt,0L,0L,
  183.        (long)(IntuiTextLength(&b_txt)+50L),70L);
  184.  
  185.    }
  186. #endif
  187.  
  188. /***************************************************
  189. *  get_fname(window,screen,hail,ddef,ddir);
  190. *
  191. *   Displays a window/requester that
  192. * gets a file name for device,directory,default file, extension
  193. *
  194. *   Calling args:
  195. * window:   Window making the request
  196. * screen:   Screen, if NULL assummed workbench
  197. * hail:   Text prompt at top of requester
  198. * ddef:   Input default file-name. Has NO DIRECTORY OR EXTENSION.
  199. * ddir:   Directory of file, may be null */
  200.  
  201.  
  202. /* Set a file-requester with prompt 'hail'   */
  203.  
  204. char *get_fname(cW,screen,hail,ddef,ddir)
  205.    struct Window *cW;        /* Calling Window   */
  206.    struct Screen *screen;    /* screen .... if null assumed workbench */
  207.    UBYTE    *hail;        /* Hailing prompt   */
  208.    char        *ddef;        /* Proable file-name   */
  209.    char        *ddir;        /* Directory in which to search   */
  210.    {
  211.    FAST struct IntuiMessage *imes;   /* Wait for message in HERE   */
  212.    FAST struct Gadget    *igad;   /* Get Gadjet Mumbo Jumbo   */
  213.    FAST long    i,class;
  214.    FAST TEXT    *pnam;
  215.  
  216.    FAST char    *retval;
  217.    FAST BOOL    dir_flag;
  218.    FAST BOOL    keepon;
  219.  
  220.    if ( ! (eW = cW) )   return(NL);
  221.  
  222.    osx.Buffer = (UBYTE *)ddef;    /* Set default file name   */
  223.    os5.Buffer = (UBYTE *)ddir;    /* Set default device name   */
  224.  
  225.    for ( i=0; i<DENTS; i++)
  226.       {
  227.       rname[i].IText = (UBYTE *)"";
  228.       rname[i].NextText = NL;
  229.       };
  230.  
  231.    NewFiles.Title = eW->Title;
  232.    if ((dir_info = AllocMem((long)sizeof(struct FileInfoBlock),0L)) == NULL)
  233.       return(NL);
  234.  
  235.    if (screen)        /* User supplied a screen */
  236.       {
  237.       NewFiles.Type = CUSTOMSCREEN;
  238.       NewFiles.Screen = screen;
  239.       }
  240.  
  241.    if ( ! (FirstEntry = (struct dirent *)AllocMem((long)DBUFSIZ,0L)) ||
  242.         ! (wRq = (struct Window *)OpenWindow( &NewFiles )) )
  243.       {
  244.       if ( FirstEntry )   FreeMem(FirstEntry,(long)DBUFSIZ);
  245.       /* notify("Can't Open Requester..."); */
  246.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  247.       return(NL);
  248.       }
  249.  
  250.    /* Set up directory, notify any errors...   */
  251.    if ( pnam = (TEXT *)dinit(ddir) )   notify(pnam);
  252.  
  253.  
  254.    /* This optional line will activate a string gadget    */
  255.    if ( IntuitionBase->lib_Version > 32 )
  256.     {
  257.     ActivateGadget(&ogx,wRq,0L);
  258.     }
  259.  
  260.    wRp = wRq->RPort;
  261.  
  262.    wRq->UserPort = eW->UserPort;
  263.    ModifyIDCMP(wRq,(long)(MOUSEBUTTONS | GADGETDOWN | GADGETUP | MOUSEMOVE));
  264.  
  265.    SetAPen(wRp,1L);
  266.    RectFill(wRp,4L,10L,(long)(RWDTH-5),(long)(RHGHT-4));
  267.  
  268.    oTxt.IText = hail;   /* Set calling arg   */
  269.    oTxt.LeftEdge = (RWDTH - IntuiTextLength(&oTxt)) >> 1L;
  270.    PrintIText(wRp,&oTxt,0L,0L);
  271.  
  272.    RefreshGadgets(&og1,wRq,(long)NL);
  273.    for ( retval= NL, keepon=TRUE; keepon ; )
  274.       {
  275.       while ( ! (imes=(struct IntuiMessage *)GetMsg(wRq->UserPort)) )
  276.       {
  277.          if ( dir_flag )
  278.             {
  279.             i = (maxent-DENTS) * cc_prop.VertPot / MAXBODY;
  280.             if ( i > (maxent-DENTS) )
  281.             i = maxent-DENTS;
  282.             if ( i <0 )   i = 0;
  283.             curent = i;
  284.             cxxx();
  285.             dir_flag = FALSE;
  286.             }
  287.          if ( more )
  288.             {
  289.             if (pnam = (TEXT *)dmore())  /* Continue to read the directory */
  290.                notify(pnam);      /* Yucko error   */
  291.             if ( maxent <= DENTS ) dir_flag = TRUE;
  292.             }
  293.          else    WaitPort(wRq->UserPort);
  294.          }
  295.       igad = (struct Gadget *)imes->IAddress;
  296.       class = imes->Class;
  297.       ReplyMsg((struct Message *)imes);
  298.  
  299.       switch (class)
  300.          {
  301.          case MOUSEMOVE:      dir_flag = TRUE;   break;
  302.       
  303.          case GADGETUP:
  304.          case GADGETDOWN:
  305.             switch ( i = igad->GadgetID)
  306.             {
  307.             case FGAD+6:
  308.             case FGAD+7:
  309.             case FGAD+8:
  310.             case FGAD+9:
  311.             case FGAD+10:       /* Replace file or directory name   */
  312.                pnam = rname[i - (FGAD+6)].IText;
  313.                if ( rname[igad->GadgetID - (FGAD+6)].NextText == NL )
  314.                   {
  315.                   RemoveGadget(wRq,&ogx);
  316.                   for (i=0; i<DSIZE; i++)      ddef[i] = *pnam++;
  317.                   AddGadget(wRq,&ogx,-1L);
  318.                   RefreshGadgets(&ogx,wRq,(long)NL);
  319.                   break;
  320.                   }
  321.                   else
  322.                   {
  323.                   RemoveGadget(wRq,&og5);
  324.                   rfnam(ddir,pnam);
  325.                   AddGadget(wRq,&og5,-1L);
  326.                   RefreshGadgets(&og5,wRq,(long)NL);
  327.                   }
  328.             case FGAD+5:
  329.                if ( pnam = (TEXT *)dinit(ddir) )
  330.                   notify(pnam);
  331.             case FGAD+3:
  332.                dir_flag = TRUE;
  333.                break;
  334.  
  335.             case FGAD+11:      /* Name gadget, OTAY gadget   */
  336.             case FGAD+1:
  337.                retval = ddef;
  338.             case FGAD+2:      /* Cancel gadget   */
  339.                keepon = FALSE;
  340.             }
  341.          }
  342.       }
  343.  
  344.       FreeMem(FirstEntry,(long)DBUFSIZ );
  345.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  346.       free_pdir();
  347.  
  348.       CloseWindowSafely(wRq);
  349.       return(retval);
  350.    }
  351.  
  352. static free_pdir()
  353.    {
  354.    if ( pdir )
  355.       {
  356.       UnLock((BPTR)pdir);
  357.       pdir = NL;
  358.       }
  359.    }
  360.  
  361. /*****************************************************************
  362. * dinit()
  363. *   Initialize the fib for directory muck.  Null return
  364. * is good, else return is a pointer to an error string      */
  365.  
  366. static char *dinit(subdir)
  367.    char *subdir;
  368.    {
  369.    more = FALSE;
  370.    curent = maxent = 0;
  371.  
  372.    NextEntry = FirstEntry;      /* Allocate from here   */
  373.    ListHead.next = NL;          /* Clear the boogie     */
  374.  
  375.    free_pdir();   /* Unlock any old lock... */
  376.  
  377.    if (! (pdir=(struct FileLock *)Lock((UBYTE *)subdir,(ULONG)ACCESS_READ)) )
  378.       return("Wrong Diskette?");
  379.    if ( ! Examine((BPTR)pdir, dir_info) )   return("Wierd Disk Error");
  380.    if ( dir_info->fib_DirEntryType < 0L )   return("Bizzare Alert!!");
  381.  
  382.    more = TRUE;
  383.    return(dmore());
  384.    }
  385.  
  386.  
  387. static char *dmore()
  388.    {
  389.    FAST struct dirent   *p_D = NextEntry;
  390.    FAST struct dirent   *ptr = (struct dirent *)&ListHead;
  391.    FAST struct dirent   *plink;
  392.  
  393.    FAST   TEXT   *p_mung;
  394.  
  395.    FAST long    i;
  396.  
  397.    if ( ! more )   return(NL);
  398.  
  399.    if ( ExNext( (BPTR)pdir, dir_info ) )
  400.       {
  401.  
  402.  
  403.       if ( (ULONG)p_D >=
  404.          ((ULONG)FirstEntry + (ULONG)DBUFSIZ - (ULONG)sizeof(struct dirent)) )
  405.          {
  406.          more = FALSE;
  407.          return("Directory Truncated!");
  408.          }
  409.  
  410.  
  411.       /* Here you can add a file/directory filter   */
  412.       /* filename text string is at &p_D->dE[0   */
  413.       p_D->isfile = ( dir_info->fib_DirEntryType < 0L );
  414.  
  415.       p_mung = (TEXT *)&p_D->dE[0];
  416.       for ( i=0; i<FCHARS; i++)
  417.          if ( ! (*p_mung++ = dir_info->fib_FileName[i]) )   break;
  418.  
  419.       i = (long)p_mung;
  420.       NextEntry = (struct dirent *)( (i+5L) & ~3L );
  421.  
  422.       for ( i=maxent++; i>=0; i--)
  423.          {
  424.          if ( ! (plink = ptr->next)  )   break;
  425.          if ( alpha_lower(p_D,plink) )   break;
  426.          ptr = plink;
  427.          }
  428.       p_D->next = plink;
  429.       ptr->next = p_D;
  430.  
  431.       return(NL);
  432.       }
  433.    else return ( IoErr() == ERROR_NO_MORE_ENTRIES) ?
  434.        (char *)(more = (long)NL) : "Error Reading Directory!!!";
  435.    }
  436.  
  437.  
  438. /* dedicated alphabetizing function for dmore()   */
  439.  
  440. static alpha_lower(snew,sold)
  441.    struct dirent *snew,*sold;
  442.    {
  443.    FAST struct dirent *pnew = snew;
  444.    FAST TEXT *ps1,*ps2, c,d;
  445.  
  446.    if ( pnew->isfile == sold->isfile)
  447.       {
  448.       ps1 = (TEXT *)&pnew->dE[0];
  449.       ps2 = (TEXT *)&sold->dE[0];
  450.       while ( (c=*ps1++) )
  451.          {
  452.          if ( c > (d=*ps2++) )   return(FALSE);
  453.          else if ( c < d )      break;
  454.          }
  455.       return(TRUE);
  456.       }
  457.    return(pnew->isfile);
  458.    }   
  459.  
  460.  
  461.  
  462. /* Display directory stuff   */
  463.  
  464. static cxxx()
  465.    {
  466.    FAST long   i,new;
  467.    FAST long   x,y;
  468.    FAST struct dirent *ohboy = (struct dirent *)&ListHead;
  469.  
  470.    new = curent;
  471.    for ( i=0; i<new; i++)   ohboy = ohboy->next;
  472.  
  473.    y = 20L;
  474.    for (i=0; i<DENTS; i++)
  475.       {
  476.       y += (x=10);
  477.       rname[i].NextText = NL;
  478.       rname[i].IText = (UBYTE *)"";
  479.       rname[i].LeftEdge = 0;
  480.       if ( (new+i) < maxent )
  481.          {
  482.          ohboy = ohboy->next;
  483.          rname[i].IText = (UBYTE *)&ohboy->dE[0];
  484.          if ( ohboy->isfile )   PrintIText(wRp,&rname[i],10L,y);
  485.          else
  486.             {
  487.             rname[i].LeftEdge = 48;
  488.             PrintIText(wRp,&saydir,10L,y);
  489.             PrintIText(wRp,&rname[i],10L,y);
  490.             rname[i].NextText = &saydir;
  491.             }
  492.          x = wRp->cp_x;
  493.          }
  494.       if ( x < ZWDTH+10 )   RectFill(wRp,x,y,(long)(ZWDTH+10),(long)(y+8L));
  495.       }
  496.    }
  497.  
  498.  
  499. /**************************************************
  500. * rfnam()
  501. *   Combines dir, plus name into dir   */
  502.  
  503. static rfnam(dir,fil_nam)
  504.    char *dir,*fil_nam;
  505.    {
  506.    FAST char   *pdst = dir;
  507.    FAST char   *psrc = fil_nam;
  508.    FAST char   c = ':';
  509.  
  510.    while ( *pdst )
  511.       c = *pdst++;
  512.    if ( c != ':')   *pdst++ = '/';
  513.  
  514.    while ( *pdst++ = *psrc++ )
  515.       ;
  516.    }
  517.  
  518. /****************************************************************/
  519. /* notify(txt)                     */
  520. /*   Prompts for Yes/No response            */
  521.  
  522. static notify(txt)
  523.    char *txt;
  524.    {
  525.    b_txt.IText = (UBYTE *)txt;
  526.    AutoRequest(wRq,&b_txt,0L,&p_txt,0L,0L,
  527.        (long)(IntuiTextLength(&b_txt)+50L),70L);
  528.  
  529.    }
  530.